<?php
/**
 * Created by PhpStorm.
 *
 * Author : Traveler.
 * Time   : 2019/12/3 18:32
 * Company: Inner Mongolia Zhengzhi Network Technology Co., Ltd
 */

namespace app\admin\controller;


use app\admin\BaseController;
use app\admin\middleware\Login;
use app\admin\model\Area as AreaModel;
use app\admin\model\Config as ConfigModel;
use app\admin\model\Icafe as IcafeModel;
use app\admin\model\IcafeCate as IcafeCateModel;
use app\admin\model\User as UserModel;
use app\admin\model\Staff as StaffModel;
use app\admin\model\StaffDept as StaffDeptModel;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{
    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [Login::class => ['except' => ['weather']]];

    /**
     * 接口：获取echart数据
     * @return \think\response\Json
     */
    public function getEchartData()
    {
        $data = [
            'icafecate' => IcafeCateModel::where('delete_time', 0)->column('name'),
            'district'  => AreaModel::where('pid', 150100)->column('name'),
            'area_data' => IcafeModel::getIcafeEchart('area'),
            'cate_data' => IcafeModel::getIcafeEchart('cate'),
            'hhht_data' => IcafeModel::getIcafeEchart('hhht')
        ];

        return $this->success('获取echart数据成功', $data);
    }

    /**
     * 页面渲染：首页
     * @return string
     */
    public function index()
    {
        // 获取系统配置
        $config = ConfigModel::getConfigByGroup(['website']);

        return View::assign('config', $config)->fetch();
    }

    /**
     * 页面渲染：天气
     * @return string
     */
    public function weather()
    {
        return View::fetch();
    }

    /**
     * 页面渲染：控制台
     * @return string
     */
    public function console()
    {
        // 平台开发相关信息
        $ibms = [
            'name'      => '综合业务管理系统',
            'author'    => 'Traveler.',
            'company'   => '内蒙古正志网络科技有限公司',
            'address'   => '呼和浩特市新城区中山东路金天帝广场17005室',
            'telephone' => '0471-6375080',
            'version'   => '内测版 v0.9.6',
            'date'      => '2020-04-20',
        ];

        // 服务器信息
        $server = [
            'server_os'      => PHP_OS,   // 服务器操作系统
            'server_env'     => $_SERVER["SERVER_SOFTWARE"],   // 运行环境
            'php_version'    => PHP_VERSION,   // PHP版本
            'php_sapi'       => PHP_SAPI,   // 运行方式
            'mysql_version'  => Db::query('select VERSION() as version')[0]['version'],   // mysql版本
            'think_version'  => $this->app->version(),   // ThinkPHP版本
            'document_root'  => $_SERVER['DOCUMENT_ROOT'],   // 当前运行脚本所在的文档根目录
            'memory_limit'   => ini_get('memory_limit'),   // 允许内存大小
            'execution_time' => ini_get('max_execution_time') . '秒',   // 最大执行时间
            'max_filesize'   => ini_get('upload_max_filesize'),   // 文件上传限制
        ];

        // 统计信息
        $count = [
            'icafe' => [
                'text' => '服务场所',
                'num' => IcafeModel::getIcafeCount()
            ],
            'user'  => [
                'text' => '前台用户',
                'num' => UserModel::getUserCount()
            ],
            'staff' => [
                'text' => '公司员工',
                'num' => StaffModel::getStaffCount()
            ],
            'staffdept' => [
                'text' => '公司部门',
                'num' => StaffDeptModel::getStaffDeptCount()
            ]
        ];

        return View::assign(['ibms' => $ibms, 'count' => $count, 'server' => $server])->fetch();
    }

    /**
     * 清理缓存
     * @return string
     */
    public function clear()
    {
        $log_path  = $this->app->getRuntimePath() . 'log' . DIRECTORY_SEPARATOR;
        $temp_path = $this->app->getRuntimePath() . 'temp' . DIRECTORY_SEPARATOR;

        // 清空缓冲池
        Cache::clear();

        // 清空应用log目录
        foreach (glob($log_path . '*', GLOB_MARK | GLOB_ONLYDIR) as $date) {
            array_map('unlink', glob($date . '*.*'));
            rmdir($date);
        }

        // 清空应用temp目录
        array_map('unlink', glob($temp_path . '*.php'));

        return $this->success('清理完成');
    }
}